#include "util/pipsutil.h"
#include "core/account.h"
#include "historyorder.h"

double HistoryOrder::getOrderCurrPrice() const
{
    return 0;
}

double HistoryOrder::getOrderClosePrice() const
{
    return m_closePrice;
}

time_t HistoryOrder::getOrderCloseTime() const
{
    return m_closeTime;
}

void HistoryOrder::setOrderCurrPrice(time_t time, double price)
{
}

bool HistoryOrder::close()
{
    return true;
}

double HistoryOrder::getOrderProfit() const
{
    double profit = 0;
    if (m_type == ORDERBUY) {
        profit = m_lots * (m_closePrice - m_openPrice) * 10 / UTIL::getPips(m_symbol);
    }
    else if (m_type == ORDERSELL) {
        profit = -m_lots * (m_closePrice - m_openPrice) * 10 / UTIL::getPips(m_symbol);
    }
    return profit;
}

double HistoryOrder::getOrderMaxProfit() const
{
    double profit = 0;
    if (m_type == ORDERBUY) {
        profit = m_lots * (m_highPrice - m_openPrice) * 10 / UTIL::getPips(m_symbol);
    }
    else if (m_type == ORDERSELL) {
        profit = -m_lots * (m_lowPrice - m_openPrice) * 10 / UTIL::getPips(m_symbol);
    }
    return profit - getOrderPoint();
}

double HistoryOrder::getOrderNetProfit() const
{
    double netProfit = 0;
    if (m_type == ORDERBUY || m_type == ORDERSELL) {
        netProfit = getOrderProfit() - getOrderPoint();
    }
    return netProfit;
}

double HistoryOrder::getOrderPoint() const
{
    return m_lots * m_fee * 10 / UTIL::getPips(m_symbol);
}

void HistoryOrder::updateBalance()
{
    if (m_type == ORDERBUY || m_type == ORDERSELL) {
        Account *account = Account::inst();
        double balance = account->getAccountBalance();
        balance += this->getOrderNetProfit();
        account->setBalance(this->m_id, balance);
        double fee = account->getAccountFee();
        fee += this->getOrderPoint();
        account->setFee(fee);
    }
}